package pl.wroc.pwr.docs.repos;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.h2.jdbcx.JdbcConnectionPool;
import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.Search;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "pl.wroc.pwr.docs.repos.*")
@EnableJpaRepositories(basePackages = "pl.wroc.pwr.docs.repos.dao")
@EnableTransactionManagement
public class AppContext extends WebMvcConfigurerAdapter {

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/static/**")
		        .addResourceLocations("/static/**");
		registry.addResourceHandler("/favicon.ico")
		        .addResourceLocations("/static/favicon.ico");
	}

	
	@Bean
	public ViewResolver viewResolver() {
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
		viewResolver.setPrefix("/WEB-INF/jsp/");
		viewResolver.setSuffix(".jsp");
		return viewResolver;
	}

	@Bean
	public DataSource dataSource() {
		/*DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName("org.h2.Driver");
		dataSource.setUrl("jdbc:h2:file:/db");
		dataSource.setUsername("");
		dataSource.setPassword("");*/
		JdbcConnectionPool cp;
		cp = JdbcConnectionPool.create("jdbc:h2:file:db", "", "");
		cp.setMaxConnections(5);
		return (DataSource) cp;
	}

	/*
	@Bean 
	public SessionFactory sessionFactory(){
		LocalSessionFactoryBuilder f = new LocalSessionFactoryBuilder(dataSource());
		f.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
		f.addAnnotatedClass(Document.class);
		return f.buildSessionFactory();
	}
	@Bean public PlatformTransactionManager transactionManager() { 
		return new HibernateTransactionManager(sessionFactory()); 
	}
	 */
	
	@Bean
	public EntityManagerFactory entityManagerFactory() {
		HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
		vendorAdapter.setGenerateDdl(true);

		LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
		factory.setJpaVendorAdapter(vendorAdapter);
		factory.setPackagesToScan("pl.wroc.pwr.docs.repos.domain");
		factory.setDataSource(dataSource());
		factory.afterPropertiesSet();

		return factory.getObject();
	}

	@Bean
	public PlatformTransactionManager transactionManager() {
		JpaTransactionManager txManager = new JpaTransactionManager();
		txManager.setEntityManagerFactory(entityManagerFactory());
		return txManager;
	}
	
	@Bean 
	public HibernateExceptionTranslator hibernateExceptionTranslator(){ 
		return new HibernateExceptionTranslator(); 
	}
	
	@Bean 
	public FullTextEntityManager fullTextEntityManager(){
		return Search.getFullTextEntityManager(entityManagerFactory().createEntityManager());
	}
}